#ifndef _LOG_TESTS_H_
#define _LOG_TESTS_H_
#include "gtest/gtest.h"
#include "testLogWriter.h"
#include <boost/shared_ptr.hpp>

class LogTestFixtureTest : public ::testing::Test
{
public:
	void SetUp(){}
	void TearDown(){}

};

/**
 * Regresion. Check that you can subscribe a listener to two appender queues.
 */
TEST_F(LogTestFixtureTest, GLOG_destroys_correctly_when_a_listener_is_suscribed_to_2_or_more_queues)
{
	glog::CGlobalLog::initSingleton();
	glog::CGlobalLog* m_log = &glog::CGlobalLog::getSingleton();
	m_log->setMiniumLevel(glog::LOGLEVEL_DEBUG);
	// add appenders
	m_log->addAppender("MY_APPENDER");
	m_log->addAppender("MY_APPENDER001");
	m_log->addAppender("MY_APPENDER002");

	// suscribing listener to 2 appenders
	//CTestLogWriter* m_tlw = new CTestLogWriter(false);
	boost::shared_ptr<glog::IListener> sp(new CTestLogWriter(false));
	m_log->addListener(sp,"MY_APPENDER001");
	m_log->addListener(sp,"MY_APPENDER002");

	//add messages to both appenders
	m_log->addMessage("MY_APPENDER002", glog::LOGLEVEL_INFO,"Esto es una linea de prueba #10");
	m_log->addMessage("MY_APPENDER002", glog::LOGLEVEL_INFO,"Esto es una linea de prueba #11");
	m_log->addMessage("MY_APPENDER002", glog::LOGLEVEL_INFO,"Esto es una linea de prueba #12");
	m_log->addMessage("MY_APPENDER002", glog::LOGLEVEL_INFO,"Esto es una linea de prueba #13");
	m_log->addMessage("MY_APPENDER001", glog::LOGLEVEL_INFO,"Esto es una linea de prueba #14");
	m_log->addMessage("MY_APPENDER001", glog::LOGLEVEL_INFO,"Esto es una linea de prueba #15");
	m_log->addMessage("MY_APPENDER001", glog::LOGLEVEL_INFO,"Esto es una linea de prueba #16");
	m_log->addMessage("MY_APPENDER001", glog::LOGLEVEL_INFO,"Esto es una linea de prueba #17");

	// destroy singleton calls the globalLog destructor
	ASSERT_NO_FATAL_FAILURE(glog::CGlobalLog::destroySingleton());
}

/**
 * Check that when  defining SILENCE macro, the log writing and processing messages functions aren't called
 */
TEST_F(LogTestFixtureTest, defining_SILENCE_macro_avoids_write_and_processing_activity_from_log)
{

	const std::string defaultAppender =  "MY_APPENDER001";
	glog::CGlobalLog::initSingleton();
	glog::CGlobalLog* m_log = &glog::CGlobalLog::getSingleton();
	m_log->setMiniumLevel(glog::LOGLEVEL_DEBUG);

	boost::shared_ptr<glog::IListener> sp(new CTestLogWriter(true));
	m_log->addListener(sp, defaultAppender);

	//add messages to log
	m_log->DEBUG_MSG(defaultAppender, "Esto es una linea de prueba con %d parametro", 1);
	m_log->WARNING_MSG(defaultAppender, "Esto es una linea de prueba con %d parametros. El segundo es el string %s", 2, "frase del dia");
	m_log->ERROR_MSG(defaultAppender, "Esto es una linea de pruebas sin paramentros");
	m_log->processMessages();
	// retrieve listener
	glog::IListener* listener = sp.get();
	// retrieve the list of messages contained in the writer
	std::list<std::string> msgList = static_cast<CTestLogWriter*>(listener)->getReportedStringList();

#ifdef GLOG_SILENCE
	ASSERT_EQ(0,msgList.size());
#else //GLOG_SILENCE
	ASSERT_EQ(3,msgList.size());
#endif //GLOG_SILENCE

	glog::CGlobalLog::destroySingleton();
}
#endif //_LOG_TESTS_H_
